<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#list entities.entity as entity>
	<#if entity.@singleton="false">
    <#assign entityName = entity.@name>
    <@pp.changeOutputFile name="${javaDir}${packageDir}/web/"+entityName?cap_first+"ControllerBase.java" />
    
<#list entity.column as column>
	<#list config.settings.setting as setting>
      	<#if column.@name=setting.@name>
			<#assign includeSettings = true>
    	</#if>
	</#list>
</#list>        	
package ${project.@rootPackage}.web;

import ${project.@rootPackage}.domain.${entityName};
import ${project.@rootPackage}.service.${entityName}Service;
    <#list entity["many-to-one"] as rel>
		<#if config.login[0]?? && rel.@entity[0] = config.login.@entity[0]>
import ${project.@rootPackage}.web.support.LoginController;
        </#if>
    </#list>
import ${project.@rootPackage}.web.util.JsfUtil;
<#list entity.column.enum as enum>
import ${project.@rootPackage}.domain.enums.${enum?parent.@type};
</#list>
<#if includeSettings>
import ${project.@rootPackage}.web.support.${config.settings.@name[0]}Controller;
</#if>  
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class for ${entityName}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
public abstract class ${entityName}ControllerBase {

	protected static final Logger logger = Logger.getLogger("${entityName}Controller");

    <#list entity["many-to-one"] as rel>
   		<#if rel.@entity != entityName>
   	@Autowired
   		<#if config.login[0]?? && rel.@entity[0] = config.login.@entity[0]>
   	protected LoginController loginController;
     		<#else>
    protected ${rel.@entity}Controller ${rel.@entity?uncap_first}Controller;
    		</#if>
    	</#if>
    </#list>
    <#list entity["many-to-many"] as rel>
        <#if !rel.@mappedBy[0]??>
	@Autowired
    protected ${rel.@entity}Controller ${rel.@entity?uncap_first}Controller;
        </#if>
    </#list>
    <#list entity["one-to-one"] as rel>
        <#if !rel.@mappedBy[0]??>
	@Autowired
    protected ${rel.@entity}Controller ${rel.@entity?uncap_first}Controller;
        </#if>
    </#list>
    
	<#if includeSettings>
	@Autowired
    protected ${config.settings.@name}Controller ${config.settings.@name[0]?uncap_first}Controller;
   	</#if>    
    @Autowired
    protected ${entityName}Service service;
    protected ${entityName} current;
    protected List<${entityName}> items = null;
       
    public ${entityName}ControllerBase() {
        //current=new ${entityName}();
    }

     /* CRUD operations */
      
    public void prepareCreate() {
        current=new ${entityName}();
	    <#list entity.column as column>
	        <#list config.settings.setting as setting>
	        	<#if column.@name=setting.@name>
	current.set${setting.@name?cap_first}(${config.settings.@name?uncap_first}Controller.get${setting.@name?cap_first}());
	    		</#if>
	    	</#list>
    	</#list>
    }

    protected void recreate() {
        prepareCreate();
        items = null;
    }

    public void save() {
    <#list entity["many-to-one"] as rel>
        <#if rel.@required[0]="true" && rel.@mode[0]="AUTO">
        	 <#if config.login[0]?? && rel.@entity[0] = config.login.@entity[0]>
       	current.set${rel.@name?cap_first}(loginController.get${config.login.@entity[0]}());
        	 <#else>
        current.set${rel.@name?cap_first}(${rel.@entity?uncap_first}Controller.getCurrent());
        	</#if>
        </#if>
    </#list>
     <#list entity["one-to-one"] as rel>
        <#if rel.@required[0]="true" && rel.@mode[0]="AUTO">
        current.set${rel.@name?cap_first}(${rel.@entity?uncap_first}Controller.getCurrent());
        </#if>
    </#list>
        if (current.isPersisted()) {
            if (service.update(current)) {
                JsfUtil.addSuccessMessage(JsfUtil.getMessage("${entityName?uncap_first}Updated"));
            }
        } else {
            if (service.create(current)) {
                JsfUtil.addSuccessMessage(JsfUtil.getMessage("${entityName?uncap_first}Created"));
            }
        }
        recreate();
    }

    public void delete() {
        if (service.delete(current)) {
            JsfUtil.addSuccessMessage(JsfUtil.getMessage("${entityName?uncap_first}Deleted"));
        }
        recreate();
    }
    
    public List<${entityName}> getItems() {
        if (items == null) {
            <#if entity.@parent[0]??>
            items = service.findBy${entity.@parent}(${entity.@parent?uncap_first}Controller.getCurrent());
            <#else>
            items = service.findAll();
            </#if>
        }
        return items;
    }
    
    <#if entity.column.@name?seq_contains("baja")>
     public List<${entityName}> getItemsNotBaja() {
        return service.findAllNotBaja();
    }
    </#if>   

    public void refreshItems() {
        items=null;
    }
 	
 	/*
     * Refresh the current ${entityName?uncap_first} and refresh items related in other beans.
     */
    public void refreshCurrent() {
        if (current != null) {
            Long id = current.getId();
            current = service.findById(id);
            <#list entity["one-to-many"] as rel>
	        ((${rel.@entity}Controller) JsfUtil.getBean("${rel.@entity?uncap_first}Controller")).refreshItems();
	        </#list>
	        <#list entity["many-to-many"] as rel>
	        ((${rel.@entity}Controller) JsfUtil.getBean("${rel.@entity?uncap_first}Controller")).refreshItems();
	        </#list>
	    }
    }
 

	<#--
	 <#list entity.column.enum as enum>
	    public List get${enum?parent.@type}Items() {
	       return Arrays.asList(${enum?parent.@type}.values());
	    }
	</#list>
	-->

	<#--
	 <#list entity["one-to-many"] as rel>
	    <#include "/includes/helperController.inc"> 
	 </#list>
	 -->

	<#list entity["many-to-many"] as rel>
    <#include "/includes/helperController.inc"> 
 	</#list>
 
 
 
    public SelectItem[] get${entityName}Items() {
   		<#if entity.@parent[0]??>
        List listItems = service.findBy${entity.@parent}(${entity.@parent?uncap_first}Controller.getCurrent());
    	<#elseif entity.column.@name?seq_contains("baja")>
    	List listItems = service.findAllNotBaja();
        <#else>
        List listItems = service.findAll();
   		</#if>
        return JsfUtil.getSelectItems(listItems,true);
    }


    
    public List<${entityName}> complete${entityName}s(String query) {
        List<${entityName}> entities = service.findAll();
        List<${entityName}> suggestions = new java.util.ArrayList<${entityName}>();
        for (${entityName} item : entities) {
            boolean add=false;
	<#list entity.column as column>
    	<#if column.@type="String">
            if (item.get${column.@name?cap_first}()!=null && item.get${column.@name?cap_first}().toUpperCase().startsWith(query.toUpperCase())) {
                add=true;
            }
       	</#if>
  	</#list>
            if (add){
                suggestions.add(item);
            }
        }
        return suggestions;
    }
    
    
    
    public ${entityName} get${entityName}() {
        return current;
    }

    public void set${entityName}(${entityName} current) {
        this.current = current;
    }

    public void setItems(List<${entityName}> items) {
        this.items = items;
    }


    public void setService(${entityName}Service service) {
        this.service = service;
    }
    
    public ${entityName}Service getService(){
        return service;
    }

    public ${entityName} getCurrent() {
        return current;
    }

    public void setCurrent(${entityName} current) {
        this.current = current;
    }

 
}
	</#if>
</#list>
